package org.viva.service.cms;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.springframework.stereotype.Service;
import org.viva.cms.sitemap.BaiduSm.SMURL;
import org.viva.core.dao.BaseDAO;
import org.viva.core.util.UTIL;
import org.viva.entity.cms.Content;
import org.viva.service.sys.WebsiteCache;

@Service("sitemapService")
public class SitemapService {

    @Resource
    private BaseDAO<Content> dao;

    @SuppressWarnings("unchecked")
    public List<SMURL> getList() {
        List<SMURL> list2 = new ArrayList<>();
        {
            SMURL l = new SMURL();
            l.setLoc(WebsiteCache.get("sys.host"));
            l.setLastmod(UTIL.FORMAT.date(new Date(), "yyyy-MM-dd"));
        }
        Session session = dao.getCurrentSession();
        {
            // 创建sql语句
            String sql = "SELECT CODE,NAME FROM CMS_CATEGORY ORDER BY ID";
            // 获得SQLQuery对象
            SQLQuery query = session.createSQLQuery(sql);
            // 设定结果结果集中的每个对象为Map类型
            query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
            // 执行查询
            List<Map<String, Object>> list = query.list();
            for (Map<String, Object> map : list) {
                SMURL l = new SMURL();
                l.setLoc(WebsiteCache.get("sys.host") + "/g/" + map.get("CODE"));
                l.setLastmod(UTIL.FORMAT.date(new Date(), "yyyy-MM-dd"));
                list2.add(l);
            }
        }
        {
            String sql = "SELECT ID,CREATE_DATE FROM CMS_CONTENT ORDER BY ID DESC";
            // 获得SQLQuery对象
            SQLQuery query = session.createSQLQuery(sql);
            // 设定结果结果集中的每个对象为Map类型
            query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
            // 执行查询
            List<Map<String, Object>> list = query.list();
            for (Map<String, Object> map : list) {
                SMURL l = new SMURL();
                l.setLoc(WebsiteCache.get("sys.host") + "/a/" + (map.get("ID") + ""));
                l.setLastmod(UTIL.FORMAT.date(map.get("CREATE_DATE"), "yyyy-MM-dd"));
                list2.add(l);
            }
        }
        return list2;
    }

}
